var express = require('express');
var router = express.Router();
var service =require('../service/service.js')
var mysql = require('mysql');


var connection = mysql.createConnection({
    host:'localhost',
    port:'3306',
    user:'root',
    password:'123456',//修改为自己的密码
    database:'sharehouse'//修改为自己的数据库
});

connection.connect(function(err){
    if (err) {      
		console.log('连接数据库出错:' + err);
		connection.end();
		return;
	};
	console.log('数据库连接成功');    
});

// 路由处理(路由加路由方法)
router.get('/', service.ShowIndex);
router.get('/index', service.ShowIndex);

// 渲染主页
router.get('/home', service.ShowHome);
//添加商品
router.post('/home',function(req,res){
    var name = req.body.name;
    var category = req.body.category;
    var price = req.body.price;
    var house = [name,category,house];
    var upload = "insert into house (name,category,price) values(?,?,?);";
    var query1 = "select * from user where name ='"+name+"' category ='"+category+"' price = '"+price+"'";
    connection.query(upload,house,function(err,result){
        if (err) throw err;
        if(result.length == 0){
            var houseinfo = house;
            console.log("添加成功");
            // console.log(result.length);
            connection.query(query1,houseinfo,function(res,req){
                if(error){
                    return;
                };
            })
            res.send({status:"success",message:"ture"});
        }else{
            res.send({status:"success",message:"false"});
        }
    })
})


//登录
router.get('/login', service.ShowLogin);

router.post('/login',function(req,res){
    var username = req.body.username;
    var password = req.body.password;
    var query1 = "select * from user where username='"+username+"' and password='"+password+"'";
    connection.query(query1,function(err,result){
        if (err) throw err;
        console.log("!!!",result.length)
        if(result.length == 0){
            res.send({status:"success"});
        }else{
            res.send({status:"failed"});
        }
    })
});

//注册
router.get('/register',service.ShowRegister);

router.post('/register',function(req,res){
    var username = req.body.username;
    var phonenum = req.body.phonenum;
    var password = req.body.password;
    var user = [username,phonenum,password];
    var insert = "insert into user (username,password,phonenum) values(?,?,?);";
    var query = "select * from user where username ='"+username+"'";
    if (username != "" && phonenum != "" && password != ""){
        connection.query(query,user,function(err,result){
            if (err) throw err;
            console.log("result",result)
            if(result.length == 0){           
                var insertInfo = user
                console.log(("注册成功"));
                connection.query(insert,insertInfo,function(error,result){
                    if(error){
                        return;
                    }; 
                })
                res.send({status:"success",message:"ture"});
            }
            else{
                console.log("注册失败");
                res.send({status:"success",message:"false"});
            }
            console.log(result.length);   
        })
    }else{
        console.log("注册信息为空")
    }
});

module.exports = router;
